package linklist;

/**
 * @author Jonny Long
 * @date 2021/2/2 21:51
 */
public class Solution2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        long sum1 = 0, sum2 = 0;
        int len1 = 0, len2 = 0;
        while (l1 != null) {
            sum1 = (long) ((l1.val * Math.pow(10, len1)) + sum1);
            len1++;
            l1 = l1.next;
        }
        while (l2 != null) {
            sum2 = (long) ((l2.val * Math.pow(10, len2)) + sum2);
            len2++;
            l2 = l2.next;
        }
        long sum = sum1 + sum2;
        int len = (sum + "").length();
        System.out.println("sum1 = " + sum1 + "\tsum2 = " + sum2);
        System.out.println(sum + "\t" + len);
        //尾插法
        ListNode head = new ListNode();
        ListNode q, p = head;
        while (len != 0) {
            q = new ListNode((int) (sum % 10), null);
            sum = sum / 10;
            p.next = q;
            p = q;
            len--;
        }
        return head.next;
    }
}
